//
//  ViewController.m
//  CollectionView
//
//  Created by GumGum on 2017/5/19.
//  Copyright © 2017年 鱼蛋蛋. All rights reserved.
//

#import "ViewController.h"
#import "CollectionViewCell.h"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)UIPageControl *pageControl;
@property(nonatomic, strong)NSArray *itemArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.itemArray = @[@"1.jpg", @"1.jpg", @"1.jpg", @"1.jpg", @"1.jpg"];
    
    NSArray *array = [[NSArray alloc] init];
    
    
    //当你的数据不是3的倍数的时候，需要自己再加上空内容的数据，达到3的倍数，这样数据不会重复显示，，
    //假如你有4个数据，，那么翻页效果会显示----0，1，2（第一页）---1，2，3（第二页），而如果添加了空内容数据翻页效果就是 0,1,2(第一页) 3,nil,nil(第二页)，假如话没有看懂可以把下面的注释掉看一下效果。
    
    if (self.itemArray.count % 3 == 1) {
        
        array = [_itemArray arrayByAddingObject:@""];
        array = [array arrayByAddingObject:@""];
        
    }
    if (self.itemArray.count % 3 == 2) {
        
        array = [_itemArray arrayByAddingObject:@""];
        
    }
    
    self.itemArray = array;
    
    CGFloat w = [UIScreen mainScreen].bounds.size.width/3.0;
    
    UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
    
    layout.itemSize = CGSizeMake(w, w + 40);//cell大小
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    layout.minimumLineSpacing = 0;//横向间距
    
    layout.minimumInteritemSpacing = 0;//纵向间距
    
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);//页边距
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, 100) collectionViewLayout:layout];
    
    _collectionView.showsHorizontalScrollIndicator = NO;
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    _collectionView.pagingEnabled = YES;
    
    [_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"recommendForYouCell"];
    
    _collectionView.delegate = self;
    
    _collectionView.dataSource = self;
    
    [self.view addSubview:_collectionView];
    
    //分页控件，本质上和scorllView没有任何关系，是2个独立的控件
    _pageControl = [[UIPageControl alloc]init];
    
    _pageControl.numberOfPages = 2;
    
    CGSize size = [_pageControl sizeForNumberOfPages:2];
    
    _pageControl.bounds = CGRectMake(0, 0, size.width, 10);
    
    _pageControl.center = CGPointMake(self.view.center.x, 290);
    
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    
    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    
    [self.view addSubview:_pageControl];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.itemArray.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *identify = @"recommendForYouCell";
    
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    cell.imgView.image = [UIImage imageNamed:_itemArray[indexPath.row]];
    
    return cell;
    
}

//设置页码
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    int page = (int)(scrollView.contentOffset.x/scrollView.frame.size.width + 0.5)%2;
    
    self.pageControl.currentPage = page;
    
}

@end
